package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "05. 属性管理模块")
@Slf4j
@RestController
@Validated
@RequestMapping("/attributes")
public class AttributeController {

    @Autowired
    IAttributeService service;

    //http://localhost:9080/albums/add-new?name=TestName&description=TestDescription&sort=23
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PostMapping(value = {"/add-new"})
    public JsonResult<Void> addNew(@Validated AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理添加属性的请求，参数：{}",attributeAddNewDTO);
        service.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除属性")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除属性的请求，参数：{}",id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据属性模板id查询属性列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "templateId",value = "属性模板id",required = true,dataType = "long")
    @GetMapping("")
    public JsonResult<List<AttributeListItemVO>> getListByTemplateId(Long templateId){
        log.debug("开始处理添加属性的请求，参数：{}",templateId);
        return JsonResult.ok(service.getListByTemplateId(templateId));
    }
}
